package com.wlcorp.forcazamis.dao.confirm.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ibatis.dao.client.DaoException;
import com.wlcorp.forcazamis.dao.confirm.IConfirmDAO;
import com.wlcorp.forcazamis.domain.confirm.Confirm;

public class ConfirmDAOImpl extends HibernateDaoSupport implements IConfirmDAO {

	@SuppressWarnings("unchecked")
	public List getConfirmList(final Confirm confirm) throws DaoException {
		final StringBuffer queryString = new StringBuffer("select confirm from Confirm as confirm where 1=1 and confirm.deleteFlag=false");
		if(null != confirm){
			if(null != confirm.getState()){
				queryString.append(" and confirm.state = ").append(confirm.getState());
			}
			if(null != confirm.getNoState()){
				queryString.append(" and confirm.state != ").append(confirm.getNoState());
			}
			if(null != confirm.getNextConfirmerId()){
				queryString.append(" and confirm.nextConfirmer.staffId = ").append(confirm.getNextConfirmerId());
			}
			queryString.append(" order by confirm.submitTime desc");
		}
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				if(null != confirm && null != confirm.getMinRownum()){
					query.setFirstResult(confirm.getMinRownum());
					query.setMaxResults(confirm.getNumPerPage());
				}
				return query.list();
			}
			
		});
	}

	public Long getConfirmCount(final Confirm confirm) throws DaoException {
		final StringBuffer queryString = new StringBuffer("select count(confirm) from Confirm as confirm where 1=1 and confirm.deleteFlag=false");
		if(null != confirm){
			if(null != confirm.getState()){
				queryString.append(" and confirm.state = ").append(confirm.getState());
			}
			if(null != confirm.getNoState()){
				queryString.append(" and confirm.state != ").append(confirm.getNoState());
			}
			if(null != confirm.getNextConfirmerId()){
				queryString.append(" and confirm.nextConfirmer.staffId = ").append(confirm.getNextConfirmerId());
			}
		}
		return (Long)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				if(null != confirm && null != confirm.getMinRownum()){
					query.setFirstResult(confirm.getMinRownum());
					query.setMaxResults(confirm.getNumPerPage());
				}
				return query.uniqueResult();
			}
			
		});
	}

	public Confirm getConfirmById(Long confirmId) throws DaoException {
		return (Confirm)getHibernateTemplate().get(Confirm.class,confirmId);
	}

	public void deleteConfirm(Confirm confirm) throws DaoException {
		confirm.setDeleteFlag(true);
		getHibernateTemplate().update(confirm);
	}

	public void saveConfirm(Confirm confirm) throws DaoException {
//		getHibernateTemplate().flush();
//		getHibernateTemplate().clear();
//		getHibernateTemplate().refresh(confirm);
//		getHibernateTemplate().saveOrUpdate(confirm);
		getHibernateTemplate().merge(confirm);
	}

}
